package com.pubinfo.passbook.engine.cell.logic.medicine.payinformation;

import com.pubinfo.passbook.common.entity.TPassbook;
import com.pubinfo.passbook.common.exception.ServiceException;
import com.pubinfo.passbook.common.mapper.TSubsidiaryInfoMapper;
import com.pubinfo.passbook.common.model.bo.witeoff.CityInfo;
import com.pubinfo.passbook.common.model.ruler.PayInformation;
import com.pubinfo.passbook.common.service.master.TPassbookService;
import com.pubinfo.passbook.common.service.ruler.CheckService;
import com.pubinfo.passbook.common.utils.ApplicationContextUilts;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

/**
 * 是否是劳模
 *
 * @author zhouch
 * @date 2022/3/14 17:11
 */
@Slf4j
public class IsLaborUnit extends PayInformationLogicUnit {

    CheckService ruleService;

    TPassbookService passbookService;

    TSubsidiaryInfoMapper infoMapper;

    @Override
    public String getName() {
        return "是否为劳模";
    }

    public IsLaborUnit() {
        ruleService = ApplicationContextUilts.getBean(CheckService.class);
        passbookService = ApplicationContextUilts.getBean(TPassbookService.class);
        infoMapper = ApplicationContextUilts.getBean(TSubsidiaryInfoMapper.class);
    }

    @Override
    public Boolean then(PayInformation data, BigDecimal result) {
        List<TPassbook> passbooks = passbookService.getPassbookById(Collections.singletonList(data.getEInvoiceId()));
        if (passbooks.isEmpty()) {
            throw new ServiceException("[是否劳模计算单元]: 找不到改票据对应的TPassbook. 计算出错.");
        }
        TPassbook passbook = passbooks.get(0);
        CityInfo info = infoMapper.findCityInfoByNsrsbh(passbook.getNsrsbh());

        String idNo = data.getPayerPartyCode();

        return ruleService.isSpecialUser(info.getDistrictName(), idNo, "internal_role");
    }
}
